<template>
  <div class="yy-put-in-manage">
    <!--列表-->
    <div class="yy-put-in-manage-list" v-show="type=='list'">
      <Card>
        <p slot="title">
          <Icon type="md-list" />
          入库管理列表
        </p>
        <Form ref="formInline" :model="formSearch"  inline  class="print-no-show">
            <FormItem prop="user">
              <Input type="text" v-model="formSearch.orderNum" placeholder="入库单号"></Input>
            </FormItem>
            <FormItem prop="user">
              <Select class="select" placeholder="请选择仓库类型" v-model="formSearch.storageNum" clearable>
              <Option v-for="(item,index) in this.currentList.cangkuA1" :value="item.storageNum" :key="index">{{item.storageName}}</Option>
              </Select>
            </FormItem>
            <FormItem prop="user">
              <Select class="select" placeholder="请选择入库类型" v-model="formSearch.inType" clearable>
                <Option v-for="(item,index) in generateInTypeArr" :value="item.catNum" :key="index">
                {{item.catName}}
              </Option>
              </Select>
            </FormItem>
            <FormItem prop="user">
              <Select class="select" placeholder="请选择审核状态" v-model="formSearch.status" clearable>
                <Option value="0">待审核</Option>
                <Option value="1">已审核</Option>
              </Select>
            </FormItem>
            <FormItem prop="user">
              <DatePicker type="date" placeholder="入库起始时间" style="width: 200px" v-model="formSearch.st" :options="startTimeOption" @on-change="onStartTimeChange"></DatePicker>
            </FormItem>
            <FormItem prop="user">
              <DatePicker type="date" placeholder="入库结束时间" style="width: 200px" v-model="formSearch.et" :options="endTimeOption" @on-change="onEndTimeChange"></DatePicker>
            </FormItem>
            <FormItem>
              <div style="width: 56px;">
                <Button type="primary" :loading="loading"  @click="handleSearch()" style="width: 100%">
                  <span v-if="!loading">查询</span>
                  <span v-else></span>
                </Button>
              </div>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleAddBuy()">采购入库</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleAddAllot()">调拨入库</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handlePrint()">打印</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" :loading="loadingExcel" @click="getExcelList()">
              <span v-if="!loadingExcel">导出</span>
              <span></span>
              </Button>
            </FormItem>
        </Form>
        <Table highlight-row stripe :columns="columns" :data="data"></Table>
        <Table highlight-row stripe :columns="columnsExcel" :data="dataExcel" ref="tables" style="display:none"></Table>
        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="formSearch.pageNum" @on-change="changePage" show-total></Page>
          </div>
        </div>
      </Card>
    </div>

    <!--详情-->
    <div class="yy-put-in-manage-details" v-show="(type=='details' || type=='audit')">
       <Card>
        <p slot="title">
          <template v-if="type=='details'"> 
            <Icon type="md-paper"/>查看详情
          </template>
          <template v-if="type=='audit'"> 
            <Icon type="md-paper"/>管理员审核
          </template>
        </p>
        <p slot="extra">
          <Button type="primary" @click="handlePrint()" style="margin-right:10px">打印</Button>
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="details">
          <p class="details-title"><span>入库单号:</span>{{currentData.orderNum}}</p>
          <p class="details-title"><span>入库类型:</span>{{currentData.inType}}</p>
          <p class="details-title"><span>供货方名称:</span>{{currentData.supName}}</p>
          <p class="details-title"><span>仓库:</span>{{currentData.storageName}}</p>
          <p class="details-title"><span>创建日期:</span>{{currentData.createTime}}</p>
          <p class="details-title"><span>审核日期:</span>{{currentData.auditTime}}</p>
          <p class="details-title"><span>制单人:</span>{{currentData.createUserName}}</p>
        </div>
        <Divider>物品列表</Divider>
        <Table highlight-row stripe :columns="columnsDetailsList" :data="detailsList" height=200 ></Table>
        <Form :model="auditSearch" v-if="type=='audit'">
          <FormItem label="审核备注" prop="user" class="textareaWidth">
            <Input type="textarea" :autosize="{minRows: 4,maxRows: 10}" v-model="auditSearch.auditMemo" placeholder="备注" maxlength="50"></Input>
          </FormItem>
          <FormItem prop="user" align="right" style="margin-right: 120px">
            <Button @click="handleAudit(currentData.orderNum)" type="primary">审核</Button>
            <Button type="primary" class="auditbutton" @click="goBack()">取消</Button>
          </FormItem>
        </Form>
      </Card>
    </div>

    <!--添加-->
    <div class="yy-put-in-manage-add-edit"  v-show="(type=='addBuy' || type=='addAllot')">
      <Card>
        <p slot="title">
          <template v-if="type=='addBuy'"> 
            <Icon type="md-create"/>采购入库清单
          </template>
          <template v-if="type=='addAllot'">
            <Icon type="md-document"/>调拨入库清单
          </template>
        </p>
        <p slot="extra" >
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="postForm">
          <Form ref="formInlineA" :model="formSearchAA"  inline>
          <FormItem prop="storageNum" v-show="this.access!=='wash'">
              <Select class="select" v-model="formSearchAA.storageNum" placeholder="请选择仓库" @on-change="changeStorage(formSearchAA.storageNum,'cangkuA1')" clearable>
                <Option v-for="(item,index) in this.currentList.cangkuA1" :value="item.storageNum" :key="index">{{item.storageName}}</Option>
              </Select>
            </FormItem>
            <FormItem prop="localNum">
              <Select class="select" placeholder="请选择库区" v-model="formSearchAA.localNum" clearable @on-change="changeStorage(formSearchAA.localNum,'cangkuA11')">
                <Option v-for="(item,index) in this.currentList.cangkuA11" :value="item.localNum" :key="index">{{item.localName}}</Option>
              </Select>
              </FormItem>
              <FormItem>
              <template v-if="type=='addBuy'"> 
                <Button type="primary" @click="handleAddBuyGoods()">添加采购物品</Button>
              </template>
              <template v-if="type=='addAllot'">
                <Button type="primary" @click="handleAddAllotGoods()">添加调拨物品</Button>
              </template>
            </FormItem>
            <div class="tip-red">注意：如果已经选择了入库仓库及物品后，重新选择入库仓库会清空已选择的物品列表。</div>         
          </Form>
          <template v-if="type=='addBuy'">
            <Divider>已选物品</Divider>
            <Table highlight-row stripe :columns="columnsA" :data="dataA" height=300></Table>
          </template>
          <template v-if="type=='addAllot'">
            <Divider>已选出库单号</Divider>
            <Table highlight-row stripe :columns="columnsAA" :data="dataAA" height=300></Table> 
          </template>

          <div class="editForm">
          <div v-show="type=='addBuy'">
            <Form ref="formInventory" :model="formInventory" :rules="checkFormInventory" :label-width="80">
              <FormItem label="供货方名称"  prop="supNum" class="inputWidth">
                <Select class="select" v-model="formInventory.supNum" placeholder="请选择供货方名称" label-in-value="true" @on-change="nameList" clearable>
                  <Option v-for="(item,index) in getSupName" :value="item.supNum" :key="index" :label="item.supName"><span style="color:gray; margin-right:10px">{{item.supNum}}</span>{{item.supName}}</Option>
                </Select>
              </FormItem>
              <FormItem label="制单时间"  prop="createTime" class="inputWidth">
                <DatePicker type="date" placeholder="制单时间" v-model="formInventory.createTime" :options="endTimeOption" @on-change="onEndTimeChange"></DatePicker>
              </FormItem>
              <FormItem label="制单备注" prop="user" class="textareaWidth">
                  <Input type="textarea" :autosize="{minRows: 2,maxRows: 10}" v-model="formInventory.inMemo" placeholder="备注" maxlength="50"></Input>
                </FormItem>
              <FormItem>
                <Button @click="createInventory()" type="primary" class="button">提交</Button>
                <Button @click="goBack()" type="primary" class="button">取消</Button>
              </FormItem>
            </Form>
          </div>

          <div v-show="type=='addAllot'">
          <Form ref="formInventory" :model="formInventory" :rules="checkFormInventory" :label-width="80">
              <FormItem label="供货方名称"  prop="supNum" class="inputWidth">
                <Select class="select" v-model="formInventory.supNum" placeholder="请选择供货方名称" label-in-value="true" @on-change="nameList" clearable>
                  <Option v-for="(item,index) in getSupName" :value="item.supNum" :key="index" :label="item.supName"><span style="color:gray; margin-right:10px">{{item.supNum}}</span>{{item.supName}}</Option>
                </Select>
              </FormItem>
              <FormItem label="制单时间"  prop="createTime" class="inputWidth">
                <DatePicker type="date" placeholder="制单时间" v-model="formInventory.createTime" :options="endTimeOption" @on-change="onEndTimeChange"></DatePicker>
              </FormItem>
              <FormItem>
                <Button @click="createInventory()" type="primary" class="button">提交</Button>
                <Button @click="goBack()" type="primary" class="button">取消</Button>
              </FormItem>
              </Form>
            </div>
          </div>
        </div>
      </Card>
    </div>

    <!--采购添加物品-->
    <Modal
      v-model="modalAddBuy"
      transfer=false
      title="选择采购入库物品"
      @on-ok="ok"
      @on-cancel="cancel"
      width="1000"
      scrollable=true
      footer-hide=true
      :mask-closable="false"
      >
      <p slot="header" >
        采购入库物品
      </p>
      
      <div class="modalAddBuyGoods">
        <Form :model="formSearchB"  inline>
          <FormItem prop="itemNum">
            <Input type="text" v-model="formSearchB.itemNum" placeholder="请输入物品编号"></Input>
          </FormItem>
          <FormItem prop="itemName">
            <Input type="text" v-model="formSearchB.itemName" placeholder="请输入物品名称"></Input>
          </FormItem>
          <FormItem prop="cateName">
            <Select class="select" v-model="formSearchB.cateName"  placeholder="请选择分类名称" clearable>
              <Option v-for="(dataSt,index) in dataCateName" :value="dataSt.cateName" :key="index" :label="dataSt.cateName"><span style="color:gray; margin-right:10px">{{dataSt.cateNum}}</span>
                {{dataSt.cateName}}
              </Option>
            </Select>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleBuyGoodsSearch()">查询</Button>
          </FormItem>
        </Form>

        <Table highlight-row stripe :columns="columnsB" :data="dataB" size="small"></Table>

        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
            <Page :total="pageB.total" :page-size="pageB.pageSize" :current="formSearchB.pageNum" @on-change="changePageB" show-total></Page>
          </div>
        </div>
          
        <Divider>完善已添加的物品属性</Divider>
        <div class="modalAddBuyGoodsEdit">
          <CheckboxGroup v-model="checkGroup">
            <div class="modal-item" v-for="(good,index) in selectGoodsList" :key="good.itemNum">
              <div class="modal-inline-a">
                <Checkbox :label="good.itemNum" :disabled="good.isChecked"><span></span></Checkbox> 
              </div>
              <div class="modal-inline-b">
                <span class="modal-item-unit"><span class="modal-title">物品名称:</span>{{good.itemName}}</span>
                <span class="modal-item-unit"><span class="modal-title">物品编号:</span>{{good.itemNum}}</span>
                <span class="modal-item-unit"><span class="modal-title">分类名称:</span>{{good.cateName}}</span>
                <span class="modal-item-unit"><span class="modal-title">规格型号:</span>{{good.size}}</span>
                <span class="modal-item-unit"><span class="modal-title">是否耐用:</span>{{good.isSpare ? "是" : "否"}}</span>
                <span class="modal-item-unit"><span class="modal-title">供应商:</span>{{good.itemName}}</span>
                <span class="modal-item-unit"><span class="modal-title">单位:</span>{{good.unitName}}</span>
                <span class="modal-item-unit"><span class="modal-title">总金额:</span>{{good.amount}}</span>
                <Form :model="good"  inline>
                  <FormItem prop="num">
                    <label class="require-input">*</label><InputNumber :min="0"  placeholder="数量" @on-change="listenInput(index,good)" v-model="good.num"></InputNumber>
                  </FormItem>
                  <FormItem prop="inPrice">
                    <label class="require-input">*</label><InputNumber :min="0"  v-model="good.inPrice" placeholder="单价" @on-change="listenInput(index,good)"></InputNumber>
                  </FormItem>
                  <FormItem prop="batchNum">
                    <label class="require-input">*</label>
                    <Select class="select" v-model="good.batchNum" placeholder="请选择品质" @on-change="listenInput(index,good)" clearable>
                      <Option value="新件">新件</Option>
                      <Option value="良品">良品</Option>
                      <Option value="坏件">坏件</Option>
                    </Select>
                  </FormItem>
                  <FormItem prop="maintainDate">
                    <label class="require-input">*</label>
                    <DatePicker type="date" placeholder="请选择质保期" class="inputWidth" v-model="good.maintainDate" @on-change="listenInput(index,good)"></DatePicker>
                  </FormItem>
                  <FormItem prop="inMemo">
                    <Input class="inMemo" type="text" v-model="good.inMemo" placeholder="备注"></Input>
                  </FormItem>
                </Form>
              </div>
              <div class="modal-inline-c">
                <Button type="error" @click="deleteGoodsList(index)">删除</Button>
              </div>
            </div>
          </CheckboxGroup>
        </div>

        <div class="modal-post">
          <Button @click="handleAddCheckList" type="primary" class="button">确认勾选</Button>
        </div>
        
      </div>
    </Modal>

    <!--调拨添加物品-->
    <Modal
      v-model="modalAddAllot"
      transfer=false
      title="选择采购入库物品"
      @on-ok="ok"
      @on-cancel="cancel"
      width="1000"
      scrollable=true
      footer-hide=true
      :mask-closable="false"
      >
      <p slot="header" >
        调拨入库物品
      </p>

      <div class="modalAddAllotGoods">
        <Form :model="formSearchC"  inline>
          <FormItem prop="orderNum">
            <Input type="text" v-model="formSearchC.orderNum" placeholder="出库单号"></Input>
          </FormItem>
          <!-- 
          <FormItem prop="outType">
            <Select class="select" placeholder="请选择出库类型" v-model="formSearchC.inoutType" clearable>
              <Option v-for="(item,index) in generateOutTypeArr" :value="item.catNum" :key="index">
                {{item.catName}}
              </Option>
            </Select>
          </FormItem>
           -->
          <FormItem prop="user">
              <DatePicker type="date" placeholder="起始时间" style="width: 200px" v-model="formSearchC.st" :options="startTimeOption" @on-change="onStartTimeChange"></DatePicker>
            </FormItem>
            <FormItem prop="user">
              <DatePicker type="date" placeholder="结束时间" style="width: 200px" v-model="formSearchC.et" :options="endTimeOption" @on-change="onEndTimeChange"></DatePicker>
            </FormItem>
          <FormItem>
            <Button type="primary" @click="handleAllotSearch()">查询</Button>
          </FormItem>
        </Form>
        <Table highlight-row stripe :columns="columnsC" :data="dataC" size="small"></Table>
        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
            <Page :total="pageC.total" :page-size="pageC.pageSize" :current="formSearchC.pageNum" @on-change="changePageC" show-total></Page>
          </div>
        </div>
        <Divider>已添加物品列表</Divider>
        <div class="modalAddAllotGoodsEdit">
          <CheckboxGroup v-model="checkAllotGroup">
            <div class="modal-item" v-for="(good,index) in selectAllotGoodsList" :key="good.orderNum">
              <div class="modal-inline-a">
                <Checkbox :label="good.orderNum" :disabled="good.isChecked"><span></span></Checkbox> 
              </div>
              <div class="modal-inline-b">
                <span class="modal-item-unit"><span class="modal-title">出库单号:</span>{{good.orderNum}}</span>
                <span class="modal-item-unit"><span class="modal-title">出库类型:</span>{{good.inoutCat}}</span>
                <span class="modal-item-unit"><span class="modal-title">材料员:</span>{{good.cusName}}</span>
                <span class="modal-item-unit"><span class="modal-title">材料员电话:</span>{{good.phone}}</span>
                <span class="modal-item-unit"><span class="modal-title">创建时间:</span>{{good.createTime}}</span>
                <span class="modal-item-unit"><span class="modal-title">审核时间:</span>{{good.auditTime}}</span>
                <span class="modal-item-unit"><span class="modal-title">制单人:</span>{{good.userName}}</span>
                <span class="modal-item-unit"><span class="modal-title">备注:</span>{{good.inMemo}}</span>
                <div class="good-list">
                  <expandRow :list="good.goodList"></expandRow>
                </div>
              </div>
              <div class="modal-inline-c">
                <Button type="error" @click="deleteAllotGoodsList(index,good)">删除</Button>
              </div>
            </div>
          </CheckboxGroup>
        </div>
        <div class="modal-post">
          <Button @click="handleAddAllotCheckList" type="primary" class="button" align>确认勾选</Button>
        </div>
      </div>

    </Modal>

    <!--打印材料入库清单 Bill of Materials-->
    <div class="yy-put-in-manage-details" v-show="type=='billMaterials'">
       <Card>
        <p slot="title">
          <Icon type="md-paper"/>
          材料{{orderReport}}单
        </p>
        <p slot="extra">
          <Button type="primary" @click="handlePrint()" style="margin-right:10px">打印</Button>
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <billMaterials :dataOrDet = dataOrDet  ref="child"></billMaterials>
      </Card>
    </div>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import { formatTime } from "@/libs/tools";
import i18n from "./locale";
import billMaterials from "../common/bill-materials/bill-materials";
import mixin from "@/mixin/date-mixin";
import expandRow from "../common/warehouse-order-expand-row-component";
import wareHouseMixin from "../common/warehouse-mixin";
import mixinTag from "@/mixin/tag-mixin";

export default {
  name: "yy-put-in-manage",
  mixins: [mixin, wareHouseMixin, mixinTag],
  components: {
    billMaterials,
    expandRow
  },
  data() {
    return {
      i18n: i18n,
      loading: false,
      loadingExcel: false,
      currentData: "",
      modalAddBuy: false,
      modalAddAllot: false,
      startTime: "",
      orderReport: "",
      inOutType: "0",
      type: "list",
      getSupName: "",
      page: {
        total: 0,
        pageSize: 20
      },
      pageB: {
        total: 0,
        pageSize: 20
      },
      pageC: {
        total: 0,
        pageSize: 20
      },
      formSearch: {
        st: "",
        et: new Date(),
        orderNum: "",
        inType: 0,
        storageNums: ["1", "2", "3"],
        userID: "",
        userName: "",
        b_UserRole: "",
        storageNum: "",
        localNum: "",
        status: "",
        igStorage: 0,
        pageNum: ""
      },
      auditSearch: {
        auditState: 1,
        auditTime: "",
        auditMemo: "",
        userID: "",
        userName: "",
        b_UserRole: "",
        igStorage: 0,
        status: ""
      },
      dataOrDet: [],
      /*dataOrDet: {
        id: "",
        outType: "",
        orderNum: "",
        createUserName: "",
        auditUserName: "",
        auditTime: "",
        supNum: "",
        supName: "",
        contactName: "",
        storageNum: "",
        storageName: "",
        localName: "",
        shippingUnit: "",
        list: []
      },*/
      formSearchAA: {
        storageNum: "",
        storageName: "",
        localNum: "",
        localName: ""
      },
      formInventory: {
        createUserName: "",
        localNum: "",
        localName: "",
        orderNum: "",
        orderNum2: "",
        inType: 0,
        supNum: "",
        supName: "",
        contactName: "",
        phone: "",
        address: "",
        storageNum: "",
        storageName: "",
        auditMemo: "",
        contractOrder: "",
        inStoreDetails: [],
        createTime: "",
        auditTime: "",
        userID: "",
        userName: "",
        b_UserRole: "",
        b_DepartNum: ""
      },
      checkFormInventory: {
        storageNum: ValidatorRule.select,
        localNum: ValidatorRule.select
        //createTime: ValidatorRule.date
      },
      formSearchB: {
        hasNum: "",
        isShowAllSpareItem: 1,
        itemName: "",
        localNum: "",
        storageNums: ["1", "2", "3"],
        userID: "",
        userName: "",
        b_UserRole: "",
        pageNum: ""
      },
      formSearchC: {
        storageNums: ["1", "2", "3"],
        orderNum: "",
        st: "",
        et: new Date(),
        inoutType: 2,
        pageNum: ""
      },
      columns: [
        {
          title: "入库单号",
          key: "orderNum",
          render: (h, params) => {
            const row = params.row;
            return h(
              "span",
              {
                class: {
                  "td-click": true
                },
                on: {
                  click: () => {
                    this.handleBillMaterials(params.index);
                  }
                }
              },
              row.orderNum
            );
          }
        },
        {
          title: "入库类型 ",
          key: "inType",
          render: (h, params) => {
            return h("span", this.generateInType(params.row.inType));
          }
        },
        {
          title: "供货方名称 ",
          key: "supName"
        },
        {
          title: "仓库 ",
          key: "storageName"
        },
        {
          title: "创建日期",
          key: "createTime"
        },
        {
          title: "审核日期",
          key: "auditTime"
        },
        {
          title: "制单人",
          key: "createUserName"
        },
        {
          title: "状态",
          key: "status",
          render: (h, params) => {
            return h("span", params.row.status == 0 ? "待审核" : "已审核");
          }
        },
        {
          title: "操作",
          key: "action",
          width: 200,
          align: "center",
          /*className: "print-no-show",*/
          render: (h, params) => {
            let viewAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.details(params.index);
                  }
                }
              },
              "查看"
            );

            let deleteAction = h(
              "Button",
              {
                props: {
                  type: "error",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.delete(params.index, params.row.id);
                  }
                }
              },
              "删除"
            );

            let repealAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.repeal(params.index, params.row.id);
                  }
                }
              },
              "撤销"
            );

            let checkingAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                },
                on: {
                  click: () => {
                    this.checking(params.index, params.row.id);
                  }
                }
              },
              "审核"
            );
            let alreadycheckAction = h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                style: {
                  marginRight: "5px"
                }
              },
              "已审核"
            );
            //根据角色判断操作按钮的显示
            let arr = "";
            if (params.row.status == 0) {
              arr = [viewAction, checkingAction, deleteAction];
            } else {
              arr = [viewAction, repealAction];
            }
            return h("div", arr);
          }
        }
      ],
      data: [],
      columnsA: [
        {
          title: "物品编码",
          key: "itemNum",
          minWidth: 100
        },
        {
          title: "物品名称",
          key: "itemName",
          minWidth: 100
        },
        {
          title: "规格型号",
          key: "size",
          minWidth: 100
        },
        {
          title: "单位",
          key: "unitName",
          minWidth: 60
        },
        {
          title: "数量",
          key: "num",
          minWidth: 60
        },
        {
          title: "入库单价",
          key: "inPrice",
          minWidth: 100
        },
        {
          title: "总金额",
          key: "amount",
          minWidth: 80
        },
        {
          title: "是否耐用",
          key: "isSpare",
          minWidth: 100,
          render: (h, params) => {
            return h("span", params.row.isSpare ? "是" : "否");
          }
        },
        {
          title: "品质",
          key: "batchNum",
          minWidth: 80
        },
        {
          title: "质保期",
          key: "maintainDate",
          minWidth: 140,
          render: (h, params) => {
            return h("span", formatTime(params.row.maintainDate));
          }
        },
        {
          title: "供货商",
          key: "itemName",
          minWidth: 100
        },
        {
          title: "仓库",
          key: "storageName",
          minWidth: 80
        },
        {
          title: "库区",
          key: "localName",
          minWidth: 80
        },
        {
          title: "联系人",
          key: "linkPerson",
          minWidth: 80
        },
        {
          title: "备注",
          key: "inMemo",
          minWidth: 60
        },
        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.deleteInventoryGood(params.index);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      dataA: [],
      columnsAA: [
        {
          type: "expand",
          width: 50,
          render: (h, params) => {
            return h(expandRow, {
              on: {
                click: () => {
                  this.getDetails(params.index);
                }
              },
              props: {
                list: params.row.goodList
              }
            });
          }
        },
        {
          title: "出库单号",
          key: "orderNum"
        },
        {
          title: "出库类型",
          key: "inoutCat"
        },
        {
          title: "材料员名称",
          key: "cusName"
        },
        {
          title: "材料员电话",
          key: "phone"
        },
        {
          title: "创建时间",
          key: "createTime"
        },
        {
          title: "审核时间",
          key: "auditTime"
        },
        {
          title: "制单人",
          key: "userName"
        },
        {
          title: "备注",
          key: "inMemo"
        },
        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.deleteInventoryGood(params.index);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      dataAA: [],
      columnsB: [
        {
          title: "物品编码",
          key: "itemNum"
        },
        {
          title: "物品名称",
          key: "itemName"
        },
        {
          title: "分类名称",
          key: "cateName"
        },
        {
          title: "规格型号",
          key: "size"
        },
        {
          title: "是否耐用",
          key: "isSpare",
          render: (h, params) => {
            return h("span", params.row.isSpare ? "是" : "否");
          }
        },
        {
          title: "单位",
          key: "unitName"
        },
        {
          title: "操作",
          key: "action",
          width: 80,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Icon",
                {
                  props: {
                    type: "md-cart",
                    size: "20",
                    color: "#5158d4"
                  },
                  style: {
                    marginRight: "5px",
                    cursor: "pointer"
                  },
                  on: {
                    click: () => {
                      this.addGoods(params.row);
                    }
                  }
                },
                ""
              )
            ]);
          }
        }
      ],
      dataB: [],
      columnsC: [
        {
          title: "出库单号",
          key: "orderNum"
        },
        {
          title: "出库类型",
          key: "inoutCat"
        },
        {
          title: "材料员名称",
          key: "cusName"
        },
        {
          title: "材料员电话",
          key: "phone"
        },
        {
          title: "创建时间",
          key: "createTime"
        },
        {
          title: "审核时间",
          key: "auditTime"
        },
        {
          title: "制单人",
          key: "createUserName"
        },
        {
          title: "备注",
          key: "inMemo"
        },
        {
          title: "操作",
          key: "action",
          width: 80,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Icon",
                {
                  props: {
                    type: "md-cart",
                    size: "20",
                    color: "#5158d4"
                  },
                  style: {
                    marginRight: "5px",
                    cursor: "pointer"
                  },
                  on: {
                    click: () => {
                      this.addAllotGoods(params.row);
                    }
                  }
                },
                ""
              )
            ]);
          }
        }
      ],
      dataC: [],
      dataCateName: [],
      selectGoodsList: [],
      selectAllotGoodsList: [
        {
          title: "出库单号",
          key: "orderNum"
        },
        {
          title: "出库类型",
          key: "outType"
        },
        {
          title: "材料员",
          key: "cusName"
        },
        {
          title: "材料员电话",
          key: "phone"
        },
        {
          title: "创建时间",
          key: "createTime"
        },
        {
          title: "审核时间",
          key: "auditTime"
        },
        {
          title: "制单人",
          key: "createUserName"
        },
        {
          title: "备注",
          key: "inMemo"
        }
      ],
      generateInTypeArr: [],
      // generateOutTypeArr: [],
      checkGroup: [],
      columnsDetailsList: [
        {
          title: "物品编号",
          key: "itemNum"
        },
        {
          title: "物品名称",
          key: "itemName"
        },
        {
          title: "规格型号",
          key: "size"
        },
        {
          title: "单位",
          key: "unitName"
        },
        {
          title: "数量",
          key: "num"
        },
        {
          title: "入库单价",
          key: "inPrice"
        },
        {
          title: "是否耐用",
          key: "isSpare",
          render: (h, params) => {
            return h("span", params.row.isSpare ? "是" : "否");
          }
        },
        {
          title: "库区",
          key: "localName"
        },
        {
          title: "品质",
          key: "batchNum"
        },
        {
          title: "质保期",
          key: "maintainDate"
        },
        {
          title: "备注",
          key: "inMemo"
        }
      ],
      detailsList: [],
      checkAllotGroup: [],
      batchNum: [],
      columnsExcel: [
        {
          title: "入库单号",
          key: "orderNum",
          render: (h, params) => {
            const row = params.row;
            return h(
              "span",
              {
                class: {
                  "td-click": true
                },
                on: {
                  click: () => {
                    this.handleBillMaterials(params.index);
                  }
                }
              },
              row.orderNum
            );
          }
        },
        {
          title: "入库类型 ",
          key: "inType"
        },
        {
          title: "供货方名称 ",
          key: "supName"
        },
        {
          title: "仓库 ",
          key: "storageName"
        },
        {
          title: "创建日期",
          key: "createTime"
        },
        {
          title: "审核日期",
          key: "auditTime"
        },
        {
          title: "制单人",
          key: "createUserName"
        },
        {
          title: "状态",
          key: "status",
          render: (h, params) => {
            return h("span", params.row.status == 0 ? "待审核" : "已审核");
          }
        }
      ],
      dataExcel: []
    };
  },

  mounted() {
    this.userInfo = this.$store.state.user.userInfo;
    this.initDate();
    this.initInType();
    // this.initOutType();
    this.handleSearch();
  },

  activated() {
    //分类名称
    this.initCateList();
    //供货方名称
    this.initgetSupNameList();
  },

  computed: {},

  methods: {
    resetInit() {
      this.handleSearch();
      this.type = "list";
    },

    initDate() {
      let date = new Date();
      let dataInt = date.getTime() - 180 * 24 * 60 * 60 * 1000;
      let startDate = new Date(dataInt);
      this.formSearch.st = startDate;
      this.formSearchC.st = startDate;
    },
    initCateList() {
      let tthis = this;
      let obj = {
        userID: tthis.userInfo.userID,
        userName: tthis.userInfo.userName,
        b_UserRole: ""
      };
      this.getCateNameList(obj);
    },

    getCateNameList(obj) {
      API.getCateNameList(obj)
        .then(res => {
          if (res.code == 200) {
            let dataCate = res.data;
            this.dataCateName = dataCate.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
          console.log("getCateNameList", err);
        });
    },

    initInType() {
      let tthis = this;
      let obj = {
        catType: 1,
        userID: tthis.userInfo.userId,
        userName: tthis.userInfo.userName,
        b_UserRole: "",
        b_DepartNum: ""
      };
      this.getInTypeList(obj);
    },

    getInTypeList(obj) {
      API.getInTypeList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            this.generateInTypeArr = data.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
          console.log("getInTypeList", err);
        });
    },

    initgetSupNameList() {
      let tthis = this;
      let obj = {
        userID: tthis.userInfo.userId,
        userName: tthis.userInfo.userName
      };
      this.getSupNameList(obj);
    },

    getSupNameList(obj) {
      API.getSupNameList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            this.getSupName = data.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
          console.log("getSupNameList", err);
        });
    },

    nameList(q) {
      if (typeof q !== "undefined") {
        this.formInventory.supName = q.label;
      } else {
        this.formInventory.supNum = "";
        this.formInventory.supName = "";
      }
    },

    handleSearch() {
      let obj = _.clone(this.formSearch);
      obj.pageNum = 1;
      obj.pageSize = this.page.pageSize;
      obj.userID = this.userInfo.userId;
      obj.userName = this.userInfo.userName;
      obj.st = this.UTC(obj.st, "starttime");
      obj.et = this.UTC(obj.et, "endtime");
      this.getList(obj);
      this.formSearch = _.clone(obj);
    },

    getList(obj) {
      this.loading = true;
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            this.loading = false;
            let data = res.data;
            obj.st = this.UTC(obj.st, "starttime");
            obj.et = this.UTC(obj.et, "endtime");
            this.formSearch = _.clone(obj);
            if (data != null) {
              this.page.total = data.total;
              this.page.pageSize = data.pageSize;
              this.data = data.list;
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.data = [];
              this.page.total = 0;
            }
          }
        })
        .catch(err => {
          this.$Message.warning("请求异常");
          console.log("getList", err);
        });
    },

    changePage(pageNum) {
      let obj = _.clone(this.formSearch),
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearch = _.clone(obj);
      this.getList(obj);
    },

    goBack() {
      if (this.type == "addBuy" || this.type == "addAllot") {
        this.$Modal.confirm({
          title: "提示：",
          content: "你确定离开吗？",
          onOk: () => {
            this.type = "list";
          }
        });
      } else {
        this.type = "list";
      }
    },

    handleReset() {
      this.$refs.formInventory.resetFields();
    },

    handleAddBuy() {
      this.formSearchAA.storageNum = "";
      this.formSearchAA.localNum = "";
      this.type = "addBuy";
      this.$refs.formInventory.resetFields();
      this.dataA = [];
      this.formInventory.createTime = new Date();
    },

    handleAddAllot() {
      this.formSearchAA.storageNum = "";
      this.formSearchAA.localNum = "";
      this.type = "addAllot";
      this.$refs.formInventory.resetFields();
      this.dataAA = [];
      this.formInventory.createTime = new Date();
    },

    details(index) {
      let tthis = this;
      this.type = "details";
      this.currentData = _.clone(this.data[index]);
      this.currentData.inType = _.clone(
        this.generateInTypeArr[this.currentData.inType - 1].catName
      );
      let obj = {
        orderNum: tthis.data[index].orderNum,
        igStorage: "",
        userID: tthis.userInfo.userId,
        userName: tthis.userInfo.userName,
        storageNum: tthis.data[index].storageNum,
        storageName: tthis.data[index].storageName,
        localNum: tthis.data[index].localNum,
        localName: tthis.data[index].localName
      };
      this.getDetailsList(obj);
    },

    // setLocalName(q) {
    //   if (typeof q !== "undefined") {
    //     this.formInventory.localName = q.label;
    //   } else {
    //     this.formInventory.localNum = "";
    //     this.formInventory.localName = "";
    //   }
    //   console.log(this.formInventory.localName);
    // },

    getDetails(index) {
      let tthis = this;
      let obj = {
        orderNum: tthis.data[index].orderNum,
        igStorage: "",
        userID: tthis.userInfo.userId,
        userName: tthis.userInfo.userName,
        storageNum: tthis.data[index].storageNum,
        storageName: tthis.data[index].storageName,
        supNum: tthis.data[index].supNum,
        supName: tthis.data[index].supName
      };
      this.getDetailsList(obj);
    },

    getDetailsList(obj) {
      API.getDetailsList(obj)
        .then(res => {
          if (res.code == 200) {
            let dataDet = res.data;
            if (dataDet != null) {
              this.detailsList = dataDet.list;
            } else {
              this.detailsList = [];
              this.$Message.success({ content: "暂无数据！" });
            }
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
          console.log("getDetailsList", err);
        });
    },

    edit() {
      this.type = "edit";
    },

    handleBillMaterials(index) {
      this.type = "billMaterials";
      this.currentData = this.data[index];
      this.dataOrDet = this.currentData;

      this.orderReport = this.generateInTypeArr[
        this.currentData.inType - 1
      ].catName;
      //请求物品详情
      this.$refs.child.orderDetails(
        this.orderReport,
        this.currentData.orderNum,
        this.inOutType
      );
    },

    generateInType(index) {
      let inTypeValue = _.find(this.generateInTypeArr, {
        catNum: index
      }).catName;
      return inTypeValue;
    },

    // generateOutType(index) {
    //   let outTypeValue = _.find(this.generateOutTypeArr, {
    //     catNum: index
    //   }).catName;
    //   return outTypeValue;
    // },

    // getBatchNum(index) {
    //   let returnBatchNum = "";
    //   if (index == 1) {
    //     returnBatchNum = "新件";
    //   } else if (index == 2) {
    //     returnBatchNum = "良品";
    //   } else {
    //     returnBatchNum = "坏件";
    //   }
    //   return returnBatchNum;
    // },

    repeal(index) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定撤销这条记录",
        onOk: function() {
          let obj = {
            orderNum: tthis.data[index].orderNum,
            userID: tthis.userInfo.userId,
            userName: tthis.userInfo.userName,
            b_UserRole: "",
            b_DepartNum: ""
          };
          API.repeal(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经撤销成功！" });
                tthis.resetList();
              }
            })
            .catch(err => {
              this.$Message.error("请求异常！");
              console.log("repeal", err);
            });
        }
      });
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            orderNum: tthis.data[index].orderNum,
            userID: tthis.userInfo.userId,
            userName: tthis.userInfo.userName
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              this.$Message.error("请求异常！");
              console.log("delete", err);
            });
        }
      });
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.formSearch.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.formSearch.pageNum !== 1
      ) {
        this.formSearch.pageNum = this.formSearch.pageNum - 1;
      }
      let obj = _.clone(this.formSearch);
      obj.st = this.UTC(obj.st, "starttime");
      obj.et = this.UTC(obj.et, "endtime");
      this.getList(obj);
    },

    checking(index) {
      this.details(index);
      this.type = "audit";
    },

    handleAudit(orderNum) {
      let obj = this.auditSearch;
      obj.orderNum = orderNum;
      obj.userID = this.userInfo.userId;
      obj.userName = this.userInfo.userName;
      this.audit(obj);
    },

    audit(obj) {
      API.audit(obj)
        .then(res => {
          if (res.code == 200) {
            this.$Message.success({ content: "审核成功！" });
            this.type = "list";
            let obj = _.clone(this.formSearch);
            obj.st = this.UTC(obj.st, "starttime");
            obj.et = this.UTC(obj.et, "endtime");
            this.getList(obj);
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
          console.log("audit", err);
        });
    },

    handleAddBuyGoods() {
      let obj = this.formSearchAA;
      if (obj.storageNum && obj.localNum) {
        this.modalAddBuy = true;
        this.checkGroup = [];
        this.selectGoodsList = [];
        if (this.dataB) {
          this.handleBuyGoodsSearch();
        }
      } else {
        this.$Message.error({ content: "请选择仓库和库区！" });
      }
    },

    handleBuyGoodsSearch() {
      let obj = this.formSearchB;
      obj.pageNum = 1;
      obj.pageSize = this.pageB.pageSize;
      obj.userID = this.userInfo.userId;
      obj.userName = this.userInfo.userName;
      obj.b_UserRole = this.userInfo.access;
      this.formSearchB = _.clone(obj);
      this.getGoodsTypeList(obj);
    },

    handleAddAllotGoods() {
      let obj = this.formSearchAA;
      if (obj.storageNum && obj.localNum) {
        this.modalAddAllot = true;
        this.checkAllotGroup = [];
        this.selectAllotGoodsList = [];
        if (this.dataC) {
          this.handleAllotSearch();
        }
      } else {
        this.$Message.error({ content: "请选择仓库和库区！" });
      }
    },

    getGoodsTypeList(obj) {
      API.getGoodsTypeList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            if (data != null) {
              this.pageB.total = data.total;
              this.pageB.pageSize = data.pageSize;
              this.dataB = data.list;
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.dataB = [];
              this.pageB.total = 0;
            }
          }
        })
        .catch(err => {
          this.$Message.warning("请求异常");
          console.log("getGoodsTypeList", err);
        });
    },

    changePageB(pageNum) {
      let obj = this.formSearchB,
        maxPageNum = Math.ceil(this.pageB.total / this.pageB.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearchB = _.clone(obj);
      this.getGoodsTypeList(obj);
    },

    addGoods(obj) {
      let bool = false,
        gId = obj.itemNum,
        arr = this.selectGoodsList;

      this.$set(obj, "isChecked", true);
      this.$set(obj, "amount", 0);
      this.$set(obj, "inPrice", "");
      this.$set(obj, "num", "");
      this.$set(obj, "localNum");
      this.$set(obj, "localName", "");

      obj.storageNum = this.formSearchAA.storageNum;
      obj.localNum = this.formSearchAA.localNum;
      obj.storageName = this.formSearchAA.storageName;
      obj.localName = this.formSearchAA.localName;

      _.forEach(arr, function(value) {
        if (value.itemNum == gId) {
          bool = true;
        }
      });

      if (!bool) {
        this.$Message.success("已添加到下方预选栏内！");
        arr.push(obj);
      } else {
        this.$Message.warning("该类型已经添加过了");
      }
      //console.log("addGoods", this.selectGoodsList);
      this.selectGoodsList = arr;
    },

    deleteGoodsList(index) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除?",
        onOk: function() {
          _.forEach(tthis.checkGroup, function(value, key) {
            //alert(2, good);
            if (value == good.orderNum) {
              tthis.checkGroup.splice(key, 1);
            }
          });
          tthis.selectGoodsList.splice(index, 1);
        }
      });
    },

    listenInput(index, obj) {
      let tthis = this;
      if (obj.num && obj.inPrice) {
        obj.amount = obj.num * obj.inPrice;
        this.selectGoodsList[index] = obj;
      } else {
        obj.amount = 0;
      }
      if (obj.num && obj.inPrice && obj.batchNum && obj.maintainDate) {
        if (_.indexOf(tthis.checkGroup, obj.itemNum) < 0) {
          this.checkGroup.push(obj.itemNum);
          this.selectGoodsList[index].isChecked = false;
        }
      } else {
        _.forEach(tthis.checkGroup, function(value, key) {
          if (value == obj.itemNum) {
            tthis.checkGroup.splice(key, 1);
          }
        });
        this.selectGoodsList[index].isChecked = true;
      }
    },

    handleAddCheckList() {
      let arr = this.checkGroup;
      if (arr.length > 0) {
        for (let i in arr) {
          let obj = _.find(this.selectGoodsList, function(o) {
            return o.itemNum == arr[i];
          });
          this.dataA.push(obj);
        }
        this.modalAddBuy = false;
        this.$Message.success("添加物品成功！");
        //console.log("确认勾选！", this.dataA);
      } else {
        this.$Message.warning("请选择采购的物品.");
      }
    },

    deleteInventoryGood(index) {
      if (this.type == "addBuy") {
        this.dataA.splice(index, 1);
      } else if (this.type == "addAllot") {
        this.dataAA.splice(index, 1);
      }
    },

    createInventory() {
      if (this.type == "addBuy") {
        this.$refs.formInventory.validate(valid => {
          if (valid) {
            if (this.dataA.length > 0) {
              let obj = this.formInventory;
              obj.userID = this.userInfo.userId;
              obj.inType = 1;
              obj.inStoreDetails = this.dataA;
              obj.storageNum = this.formSearchAA.storageNum;
              obj.localNum = this.formSearchAA.localNum;
              obj.storageName = this.formSearchAA.storageName;
              obj.localName = this.formSearchAA.localName;
              // obj.createUserName = this.userInfo.userName;
              obj.userName = this.userInfo.userName;
              obj.createTime = "";

              API.saveInventory(obj)
                .then(res => {
                  if (res.code == 200) {
                    this.$Message.success("添加成功！");
                    //提交以后重置表单
                    this.$refs.formInventory.resetFields();
                    this.dataA = [];
                    this.dataB = [];
                    this.formInventory.createTime = new Date();
                    //回到List页面，重新请求
                    this.type = "list";
                    this.handleSearch();
                  }
                })
                .catch(err => {
                  this.$Message.error("请求异常！");
                  console.log("createInventory", err);
                });
            } else {
              this.$Message.warning("请选择采购的物品.");
            }
          }
        });
      }

      if (this.type == "addAllot") {
        this.$refs.formInventory.validate(valid => {
          if (valid) {
            if (this.dataAA.length > 0) {
              let obj = this.formInventory;
              obj.userID = this.userInfo.userId;
              obj.inType = 3;
              obj.inStoreDetails = this.fliterGoodList(this.dataAA);
              obj.storageNum = this.formSearchAA.storageNum;
              obj.localNum = this.formSearchAA.localNum;
              obj.storageName = this.formSearchAA.storageName;
              obj.localName = this.formSearchAA.localName;
              console.log(obj.localName);
              obj.userName = this.userInfo.userName;
              obj.createTime = "";

              API.saveInventory(obj)
                .then(res => {
                  if (res.code == 200) {
                    this.$Message.success("添加成功！");
                    //提交以后重置表单
                    this.$refs.formInventory.resetFields();
                    this.dataAA = [];
                    this.formInventory.createTime = new Date();
                    //回到List页面，重新请求
                    this.type = "list";
                    // this.getList(this.formSearch);
                    this.handleSearch();
                  }
                })
                .catch(err => {
                  this.$Message.error("请求异常！");
                  console.log("createInventory", err);
                });
            } else {
              this.$Message.warning("请选择调拨出库单.");
            }
          } else {
            this.$Message.warning("请选择填写完清单.");
          }
        });
      }
    },

    fliterGoodList(list) {
      let arr = [];
      for (var i in list) {
        arr = arr.concat(list[i].goodList);
      }
      return arr;
    },

    getSaveInventoryGoods(obj) {
      let list = [];
      for (let i in obj) {
        let item = obj[i].list;
        for (let j in item) {
          list.push(item[j]);
        }
      }
      return list;
    },

    handleAllotSearch(orderNum) {
      let obj = _.clone(this.formSearchC);
      obj.pageNum = 1;
      obj.pageSize = this.pageC.pageSize;
      obj.storageNum = this.formInventory.storageNum;
      //obj.localNum = this.formInventory.localNum;
      //obj.userID = this.userInfo.userId;
      //obj.userName = this.userInfo.userName;
      //obj.b_UserRole = this.userInfo.access;
      //this.$refs.child.orderDetails(this.currentData.orderNum);
      obj.st = this.UTC(obj.st, "starttime");
      obj.et = this.UTC(obj.et, "endtime");
      this.getOutInStorage(obj);
      this.formSearchC = _.clone(obj);
    },

    getOutInStorage(obj) {
      API.getOutInStorage(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            obj.st = this.UTC(obj.st, "starttime");
            obj.et = this.UTC(obj.et, "endtime");
            this.formSearchC = _.clone(obj);
            if (data != null) {
              this.pageC.total = data.total;
              this.pageC.pageSize = data.pageSize;
              this.dataC = data.list;
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.dataC = [];
              this.pageC.total = 0;
            }
          }
        })
        .catch(err => {
          this.$Message.warning("请求异常");
        });
    },

    changePageC(pageNum) {
      let obj = this.formSearchC,
        maxPageNum = Math.ceil(this.pageC.total / this.pageC.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearchC = _.clone(obj);
      this.getOutInStorage(obj);
    },

    addAllotGoods(obj) {
      //console.log("addAllotGoods", obj);
      let bool = false,
        gId = obj.orderNum,
        arr = this.selectAllotGoodsList;

      //默认时能勾选
      this.$set(obj, "isChecked", false);
      this.$set(obj, "goodList", []);

      _.forEach(arr, function(value) {
        if (value.orderNum == gId) {
          bool = true;
        }
      });

      if (!bool) {
        this.$Message.success("已添加到下方预选栏内！");
        arr.push(obj);
        this.getAllotGoodList(obj);
      } else {
        this.$Message.warning("该单号已经添加过了");
      }
      //console.log(1953,arr);
      this.selectAllotGoodsList = arr;
      obj.userName = this.userInfo.userName;
      this.checkAllotGroup.push(obj.orderNum);
    },

    getAllotGoodList(obj) {
      let apiObj = {
        b_UserRole: "",
        orderNum: obj.orderNum,
        userID: "",
        userName: ""
      };

      API.getAllotGoodList(apiObj)
        .then(res => {
          if (res.code == 200 && res.data) {
            let data = res.data;
            if (data.list) {
              let list = data.list,
                oneObj = _.find(this.selectAllotGoodsList, {
                  orderNum: obj.orderNum
                });
              oneObj.goodList = data.list.concat(oneObj.goodList);
              _.forEach(oneObj.goodList, function(obj) {
                obj.inMemo = "工单" + obj.orderNum + "自动出库";
                obj.inPrice = obj.outPrice;
              });
            }
          } else {
            this.$Message.error("在该单号下,没有找到物品，请核查！");
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    deleteAllotGoodsList(index, good) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除?",
        onOk: function() {
          _.forEach(tthis.checkAllotGroup, function(value, key) {
            if (value == good.orderNum) {
              tthis.checkAllotGroup.splice(key, 1);
            }
          });
          tthis.selectAllotGoodsList.splice(index, 1);
        }
      });
    },

    handleAddAllotCheckList() {
      let arr = this.checkAllotGroup;
      if (arr.length > 0) {
        for (let i in arr) {
          let obj = _.find(this.selectAllotGoodsList, function(o) {
            return o.orderNum == arr[i];
          });
          let isHava = _.find(this.dataAA, function(o) {
            return o.orderNum == arr[i];
          });
          if (!isHava) {
            this.dataAA.push(obj);
          } else {
            this.$Message.error({
              content: "单号" + arr[i] + "已经添加了，请勿重复添加！",
              top: 50,
              duration: 3
            });
          }
        }
        this.modalAddAllot = false;
        this.$Message.success("添加物品成功！");
        //console.log("确认勾选！", this.dataAA);
        //console.log(8888, this.dataAA.goodList);
      } else {
        this.$Message.warning("请选择调拨的物品.");
      }
    },

    handlePrint() {
      window.print();
    },

    getExcelList() {
      let obj = _.clone(this.formSearch);
      obj.pageNum = 1;
      obj.pageSize = this.page.total;
      console.log("导出数据的条数", obj.pageSize);
      obj.userID = this.userInfo.userId;
      obj.st = this.UTC(obj.st, "starttime");
      obj.et = this.UTC(obj.et, "endtime");
      if (this.loadingExcel == false) {
        this.getListA(obj);
      }
    },

    getListA(obj) {
      this.loadingExcel = true;
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            this.loadingExcel = false;
            let data = res.data;
            if (data != null) {
              this.dataExcel = data.list;
              this.statusExcel();
              this.handleExcel();
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.dataExcel = [];
            }
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    handleExcel() {
      this.$refs.tables.exportCsv({
        filename: "入库管理列表数据",
        columns: this.columnsExcel,
        data: this.dataExcel
      });
      this.$Message.success({ content: "导出Excel成功！" });
      this.dataExcel = [];
    },

    statusExcel() {
      for (let i = 0; i < this.dataExcel.length; i++) {
        if (this.dataExcel[i].status == 0) {
          this.dataExcel[i].status = "待审核";
        } else {
          this.dataExcel[i].status = "已审核";
        }

        this.dataExcel[i].inType = _.find(this.generateInTypeArr, {
          catNum: this.dataExcel[i].inType
        }).catName;
      }
    }
  }
};
</script>
<style lang="less" >
@import "./yy-put-in-manage.less";
</style>
